package com.zhoulz.homework.chaptor02.node;

import java.util.Calendar;
import java.util.Date;

/**
 * 接受到的文件
 * @author zhoulz
 *
 */
public class ReceiveFile {
	private String nodeName;
	private String fileName;
	private long fileLength;
	private Date receiveTime;
	private int receiveProcess;
	
	public ReceiveFile(String nodeName, String fileName, long fileLength,
			Date receiveTime, int receiveProcess) {
		super();
		this.nodeName = nodeName;
		this.fileName = fileName;
		this.fileLength = fileLength;
		this.receiveTime = receiveTime;
		this.receiveProcess = receiveProcess;
	}
	public String getNodeName() {
		return nodeName;
	}
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public long getFileLength() {
		return fileLength;
	}
	public void setFileLength(long fileLength) {
		this.fileLength = fileLength;
	}
	public Date getReceiveTime() {
		return receiveTime;
	}
	public void setReceiveTime(Date receiveTime) {
		this.receiveTime = receiveTime;
	}
	public int getReceiveProcess() {
		return receiveProcess;
	}
	public void setReceiveProcess(int receiveProcess) {
		this.receiveProcess = receiveProcess;
	}
	
	public static final String header = new StringBuilder().append("Node").append("\t\t")
			.append("File").append("\t")
			.append("Size").append("\t")
			.append("Process").append("\t")
			.append("Time")
			.toString();
	@Override
	public String toString() {
		return new StringBuilder().append(this.nodeName).append("\t")
				.append(this.fileName).append("\t")
				.append(formatSize(this.fileLength)).append("\t")
				.append(this.receiveProcess).append("%").append("\t")
				.append(formatDate(Calendar.getInstance().getTimeInMillis() - this.receiveTime.getTime())).append(" ago")
				.toString();
	}
	private String formatSize(long size){
		
		if(size < 1024 * 1024){
			return size/1024 + "kb";
		}else if(size < 1024 * 1024 * 1024){
			return size/1024/1024 + "mb";
		}else{
			return size/1024/1024/1024 + "gb";
		}
	}
	private String formatDate(long mills){
		if(mills < 1000){
			return mills + "ms";
		}else if(mills < 60 * 1000){
			return mills/1000 + "s";
		}else if(mills < 60 * 60 * 1000){
			return mills/(60 * 1000) + "min";
		}else if(mills < 24 * 60 * 60 * 1000){
			return mills/(60 * 60 * 1000) + "h";
		}else{
			return mills/(24 * 60 * 60 * 1000) + "days";
		}
	}
}
